var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var in_task = require("./inTask");
var point_map = require("./pointMap");

var VIDEO_CONST_WATCH_VIDEO_TIMES = 1;

var video = {
  go_page_from_home() {
    if (utils.exists_text("视频") && utils.exists_text("小说")) {
      console.log("video:go_page_from_home");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页视频"])
      );
      sleep(2 * 1000);
      this.init();
    } else {
      console.log("video go_page_from_home: NOT FOUND");
    }
  },
  init() {
    console.log("video init: ");
    this.run();
  },
  run() {
    console.log("video run:");
    console.show();
    var watch_times = VIDEO_CONST_WATCH_VIDEO_TIMES + random(0, 2);
    console.log("video run: 即将开始看本轮视频，共" + watch_times + "个视频");
    while (watch_times > 0) {
      var swipe_interval = 700 + random(10, 20);
      console.log("video run: 开始滑动---");
      swipe(
        parseInt(device.width / 2),
        parseInt((device.height / 10) * 8),
        parseInt(device.width / 2),
        parseInt((device.height / 10) * 1),
        swipe_interval
      );
      sleep(swipe_interval);
      console.log("video run: 滑动结束---");
      var interval = 4 + random(1, 3);
      console.log("video run: 看视频" + interval + "秒");
      sleep(interval * 1000);
      console.log(
        "video run: 已看完第" +
          watch_times +
          "个视频，下面开始判断是否点赞或收藏"
      );
      var a_or_c_flag = random(1, 5);
      if (a_or_c_flag == 1) {
        if (utils.exists_text_starts_with("相关搜索")) {
          console.log("video run: " + a_or_c_flag, "找到【相关搜索】");
        } else if (utils.exists_text_starts_with("下一条")) {
          console.log("video run: " + a_or_c_flag, "找到【下一条】");
        } else if (utils.exists_text_starts_with("点击进入直播间")) {
          console.log("video run: " + a_or_c_flag, "找到【点击进入直播间】");
        } else {
          console.log("video run: " + a_or_c_flag, "开始点赞");
          utils.click_point.apply(
            utils,
            utils.get_size_point([624, 1005, 696, 1132])
          );
        }
        sleep(2 * 1000);
      }
      if (a_or_c_flag == 3) {
        if (utils.exists_text_starts_with("相关搜索")) {
          console.log("video run: " + a_or_c_flag, "找到【相关搜索】");
        } else if (utils.exists_text_starts_with("下一条")) {
          console.log("video run: " + a_or_c_flag, "找到【下一条】");
        } else if (utils.exists_text_starts_with("点击进入直播间")) {
          console.log("video run: " + a_or_c_flag, "找到【点击进入直播间】");
        } else {
          console.log("video run: " + a_or_c_flag, " 开始收藏");
          utils.click_point.apply(
            utils,
            utils.get_size_point([624, 1259, 696, 1386])
          );
        }
        sleep(2 * 1000);
      }
      watch_times = watch_times - 1;
    }
    console.log("video run: 已看完本轮视频");
    sleep(1 * 1000);
    console.hide();
  },
  after() {
    console.log("video after:");
    this.next();
  },
  next() {
    console.log("video next:");
  },
};
module.exports = video;
